
#include "main.h"
#include <stdbool.h>
/* Private variables ---------------------------------------------------------*/
USART_HandleTypeDef husart2;

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART2_Init(void);
// 全局变量，用于标记是否收到特定命令



/**
  * @brief  The application entry point.
  * @retval int
*/#include "main.h"
#include <string.h>
#include <stdio.h>  // 引入sprintf函数的头文件
#include <stdlib.h> // 引入随机数函数库
USART_HandleTypeDef husart2;

// 函数声明
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART2_Init(void);
void Error_Handler(void);
void sendData(const char *data);
void generate_random_health_data(int *steps, float *temperature, int *heartRate, int *breathing);
bool badCommandReceived = false; // 在此文件中定义变量


int main(void)
{
  // 初始化HAL库
  HAL_Init();

  // 配置系统时钟
  SystemClock_Config();

  // 初始化GPIO和USART2
  MX_GPIO_Init();
  MX_USART2_Init();

  int totalSteps = 0;
  float temperature = 0.0;
  int heartRate = 0;
  int breathing = 0;

  // 主循环
  while (1)
  {
    // 生成随机健康数据并累加步数
    generate_random_health_data(&totalSteps, &temperature, &heartRate, &breathing);

    // 构造JSON字符串
    char jsonData[256]; // 用来存储JSON数据的缓冲区
    sprintf(jsonData, "{\"steps\": %d, \"temperature\": %.1f, \"heartRate\": %d, \"breathing\": %d}",
            totalSteps, temperature, heartRate, breathing); // 格式化成JSON格式的字符串

    // 发送构造好的JSON数据到USART2
    sendData(jsonData);

    // 添加一些延迟，避免数据发送过快
    HAL_Delay(1000); // 调整延迟时间，单位为毫秒
  }
}
void generate_random_health_data(int *steps, float *temperature, int *heartRate, int *breathing)
{
  // 步数增加：30% 的概率增加 1，70% 的概率增加 0
  if (rand() % 100 < 3) {
    *steps += 1;
  }

  // 随机体温：36.0到37.5摄氏度
  *temperature = 36.0f + (rand() % 16) / 10.0f;

  // 根据是否收到特定命令调整心率和呼吸
  if (badCommandReceived) {
    *heartRate = 2;  // 心率设置为2，假定是错误命令下的特定值
    *breathing = 2;  // 呼吸频率设置为2，假定是错误命令下的特定值
  } else {
    // 随机心率：60到100次/分钟
    *heartRate = 60 + rand() % 41;

    // 随机呼吸频率：12到20次/分钟
    *breathing = 12 + rand() % 9;
  }
}

void USART2_IRQHandler(void)
{
  uint8_t received_data;
  if (__HAL_USART_GET_FLAG(&husart2, USART_FLAG_RXNE))
  {
    // Read one byte from the receive data register
    received_data = (uint8_t)(husart2.Instance->DR & 0xFF);

    // Send the received data back
    HAL_USART_Transmit(&husart2, &received_data, 1, 10);
  }
  HAL_USART_IRQHandler(&husart2);
}



void Error_Handler(void)
{
  printf("System will restart due to fatal error.\n");

  // 调用 NVIC 系统重启函数
  NVIC_SystemReset();
}




// 发送数据到USART2
void sendData(const char *data)
{
  // 获取数据的长度
  uint16_t dataLength = strlen(data);

  // 发送数据
  if (HAL_USART_Transmit(&husart2, (uint8_t *)data, dataLength, HAL_MAX_DELAY) != HAL_OK)
  {

  }
}


/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2;
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL12;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  {
    Error_Handler();
  }
}

/**
  * @brief USART2 Initialization Function
  * @param None
  * @retval None
  */
static void MX_USART2_Init(void)
{

  husart2.Instance = USART2;
  husart2.Init.BaudRate = 9600;
  husart2.Init.WordLength = USART_WORDLENGTH_8B;
  husart2.Init.StopBits = USART_STOPBITS_1;
  husart2.Init.Parity = USART_PARITY_NONE;
  husart2.Init.Mode = USART_MODE_TX_RX;
  husart2.Init.CLKPolarity = USART_POLARITY_LOW;
  husart2.Init.CLKPhase = USART_PHASE_1EDGE;
  husart2.Init.CLKLastBit = USART_LASTBIT_DISABLE;
  if (HAL_USART_Init(&husart2) != HAL_OK)
  {
    Error_Handler();
  }

  __HAL_USART_ENABLE_IT(&husart2, USART_IT_RXNE);
  // 设置USART2中断优先级
  HAL_NVIC_SetPriority(USART2_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(USART2_IRQn);
  // 使能USART2全局中断


}

/**
  * @brief GPIO Initialization Function
  * @param None
  * @retval None
  */
static void MX_GPIO_Init(void) {
  /* USER CODE BEGIN MX_GPIO_Init_1 */
  /* USER CODE END MX_GPIO_Init_1 */

  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOD_CLK_ENABLE();
  __HAL_RCC_GPIOA_CLK_ENABLE();
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{

}
#endif /* USE_FULL_ASSERT */
